
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

# NOTE: Uses code from Stargazer Package Updates.R instead of the published version of Stargazer. See readme.txt.

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Table 2 - Effects of Framing on Attitudes Toward Withdrawal #################

### H1

# H1: Public support for withdrawal will be reduced when leaving is framed as a victory for the enemy in the conflict.
# logit withdraw_yn taliban
data$taliban_b <- ifelse(data$taliban == "2", 1, 0)
data$framing_b <- ifelse(data$framing == "2", 1, 0)

h1 <- glm (withdraw_yn ~ taliban, data = data, family="binomial")
summary(h1)
h1 %>% coef() %>% exp()

h1.1 <- glm (withdraw_yn ~ taliban_b, data = data, family="binomial")

data$t_frame <- data$taliban_b*data$framing_b

h1.2 <- glm (withdraw_yn ~ taliban_b + framing_b + t_frame, data = data, family="binomial")

h1_tab <- data.frame(taliban=0:1)
h1_tab <- cbind(h1_tab,predict(h1, newdata=h1_tab, type="response", se.fit=TRUE))
h1_tab$lower = h1_tab$fit - 1.96*h1_tab$se.fit
h1_tab$upper = h1_tab$fit + 1.96*h1_tab$se.fit
h1_tab

f2a <- h1_tab %>% dplyr::select(taliban, fit, lower, upper)
f2a$table <- "Enemy Victory Frame\nDV: Withdraw"
names(f2a)[names(f2a) == 'taliban'] <- 'binary'
f2a["binary"][f2a["binary"] == "0"] <- "No"
f2a["binary"][f2a["binary"] == "1"] <- "Yes"

logitmfx(withdraw_yn ~ taliban, data=data)

taliban <-  data[(data$taliban == "2"),]


### H2
# H2: Public support for staying the course in a conflict will go up when it is framed as a middling policy option rather than the most interventionist choice. 
# logit staycourse_yn framing
h2 <- glm (staycourse_yn ~ framing, data = data, family="binomial")
summary(h2)
h2 %>% coef() %>% exp()

h2.1 <- glm (staycourse_yn ~ framing_b, data = data, family="binomial")

h2.2 <- glm (staycourse_yn ~ taliban_b + framing_b + t_frame, data = data, family="binomial")

h2_tab <- data.frame(framing=0:1)
h2_tab <- cbind(h2_tab,predict(h2, newdata=h2_tab, type="response", se.fit=TRUE))
h2_tab$lower = h2_tab$fit - 1.96*h2_tab$se.fit
h2_tab$upper = h2_tab$fit + 1.96*h2_tab$se.fit
h2_tab

f2b <- h2_tab %>% dplyr::select(framing, fit, lower, upper)
f2b$table <- "Middle Ground Frame\nDV: Stay the Course"
names(f2b)[names(f2b) == 'framing'] <- 'binary'
f2b["binary"][f2b["binary"] == "0"] <- "No"
f2b["binary"][f2b["binary"] == "1"] <- "Yes"

f2 <- rbind(f2a, f2b)
rm(f2a, f2b)
f2$binary <- as.factor(f2$binary)

f2$fit_label <- paste0(format(round(f2$fit*100, 0), nsmall = 0), "%")


logitmfx(withdraw_yn ~ framing, data=data)


### Table 2
output <- stargazer(h1.1, h2.1,  title="Table 2: Effects of Framing on Attitudes Toward Withdrawal",
                    align=TRUE, dep.var.labels=c("Withdraw", "Stay the Course"),
                    column.labels=c("",""),
                    digits = 3, digits.extra = 0,
                    notes = "P-value shown in parentheses below coefficient", 
                    notes.append = FALSE,
                    report = "vc*p",
                    covariate.labels=c("Enemy Victory Frame&emsp;&emsp;", "Middle Ground Frame", "Constant"),
                    keep.stat=c("n","chi2"), no.space=TRUE, notes.align="c", out = "Plots/Table 2 - Effects of Framing on Attitudes Toward Withdrawal.html")

webshot("Plots/Table 2 - Effects of Framing on Attitudes Toward Withdrawal.html", "Plots/Table 2 - Effects of Framing on Attitudes Toward Withdrawal.png", zoom=5, vwidth=415, vheight=300)

rm(f2, h1, h1_tab, h1.1, h1.2, h2, h2_tab, h2.1, h2.2, taliban, output)
